<style>
    form[lay-filter="qualityTestingTypeForm"] .layui-form-label{
        width: 100px;
    }
    form[lay-filter="qualityTestingTypeForm"] .layui-input-block{
        margin-left: 130px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline">
                                        <select name="status">
                                            <option value="">请选择状态</option>
                                            <option value="enable">启用</option>
                                            <option value="unable">停用</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="qualityTestingTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="qualityTestingTypeTable" lay-filter="qualityTestingTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="qualityTestingTypeTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="qualityTestingTypeForm">
    <form lay-filter="qualityTestingTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">所属业务性质</label>
            <div class="layui-input-block">
                <select  name="businessProperty" lay-filter="businessProperty" lay-search lay-verify="required" >
                    <option value="">请选择</option>
                    <option value="0">采购管理</option>
                    <option value="1">生产管理</option>
                    <option value="2">销售管理</option>
                    <option value="3">其他业务</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">适用物资类型</label>
            <div class="layui-input-block">
                <select id="productTypeId" name="productTypeId" lay-filter="productTypeId" lay-search lay-verify="required" >

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                    <option value="enable">启用</option>
                    <option value="unable">停用</option>
                </select>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea" lay-verify="" required></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="qualityTestingTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#qualityTestingTypeTable',
            url: config.base + 'production/qualityTesting/admin/qualityTestingType/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'name',title: '名称'}
                ,{field: 'statusDesc',  title: '状态'}
                ,{field: 'remark',  title: '描述'}
                ,{field: 'creatorName', title: '创建人'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#qualityTestingTypeTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(qualityTestingTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(qualityTestingTypeTable)', function(obj){
            table.reload('qualityTestingTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#qualityTestingTypeBtnAdd').click(function () {
            showEditModel();
        });

        form.render('select');

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '660px',
                offset: '65px',
                title: data ? '修改质检类型' : '添加质检类型',
                content: $('#qualityTestingTypeForm').html(),
                success: function () {
                    admin.reqSync('enterprise/product/admin/productType/getAllKnife',{},function (res) {
                        if (res.code == 200) {
                            $.each(res.data,function (index,item) {
                                $("#productTypeId").append(new Option(item.name,item.id));
                            })
                            form.render('select');
                        }else{
                            layer.msg(res.msg, {icon: 2});
                        }

                    },'GET')
                    if(data) {
                        var select = 'dd[lay-value=' + data.productTypeId + ']';
                        $("select[name='productTypeId']").siblings("div.layui-form-select").find('dl').find(select).click();
                    }
                    form.val('qualityTestingTypeForm', data);
                    // 表单提交事件
                    form.on('submit(qualityTestingTypeFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'production/qualityTesting/admin/qualityTestingType/update' : 'production/qualityTesting/admin/qualityTestingType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('qualityTestingTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('production/qualityTesting/admin/qualityTestingType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#qualityTestingTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('qualityTestingTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>